﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using System.Drawing;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_RevealSecrets : TargetSpell
    {
        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, false, out MapItemsWithin12) == false)
                return;

            List<Point> hiddenList = CreatureAI.FindHidden(myCreatureBase, myCreatureBase.currentMapItem.mapItemsWithin4, false);
            List<MapItem> mapItemsToUpdate = new List<MapItem>();
            List<Character> charsToUpdate = new List<Character>();
            foreach (MapItem mapItem in myCreatureBase.currentMapItem.mapItemsWithin12)
            {
                //Find secret doors that are not open
                if (Math.Abs(mapItem.MapX - myCreatureBase.MapX) <= 4 && Math.Abs(mapItem.MapY - myCreatureBase.MapY) <= 4 &&
                    mapItem.TerrainType == "SDoor" && mapItem.TerrainUrl.IndexOf("Open") == -1)
                {
                    //Make sure that they're in our line of sight
                    if (CreatureAI.isHidden(mapItem.MapX, mapItem.MapY, hiddenList) == false)
                    {
                        mapItem.Transparent = true;
                        mapItem.Motion = 4;
                        mapItem.TerrainUrl = mapItem.TerrainUrl + "_Open";

                        mapItem.timerHexReset.Stop();
                        mapItem.timerHexReset.Start();

                        mapItemsToUpdate.Add(mapItem);
                    }
                }
                foreach (CreatureBase creatureBase in mapItem.CreaturesBases)
                {
                    if (creatureBase is Character)
                        charsToUpdate.Add((Character)creatureBase);
                }
            }

            if (mapItemsToUpdate.Count > 0)
            {
                foreach (Character character in charsToUpdate)
                {
                    foreach (MapItem mapItem in mapItemsToUpdate)
                        character.PlaySound("DoorOpen", mapItem.MapX, mapItem.MapY);

                    character.AddMapItems(mapItemsToUpdate, false);
                }
            }
        }
    }
}
